package com.huangmb.anymark.app.fragment.marker;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.*;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.huangmb.androidlib.utils.Logger;
import com.huangmb.anymark.app.R;
import com.huangmb.anymark.app.fragment.AppBaseFragment;
import com.huangmb.anymark.app.fragment.BaseMapFragment;
import com.huangmb.anymark.app.model.UserMarker;
import com.huangmb.anymark.app.utils.GeoSearchUtils;

/**
 * @author hmb
 * @version 1.0
 * @since 2016/4/24 14:21
 */
public class BaseUserMarkerFragment extends AppBaseFragment {
    private final String TAG = BaseUserMarkerFragment.class.getSimpleName();

    protected String mStaticMapUrl = "http://restapi.amap.com/v3/staticmap?zoom=17" +
            "&size=750*300&markers=mid,0xFC6054,A:%f,%f&key=ee95e52bf08006f63fd29bcfbcf21df0";

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    public UserMarker getUserMarker(){
        return getArguments().getParcelable("marker");
    }

    protected void queryStaticMap(final ImageView mapView, double latitude, double longitude) {
        String url = String.format(mStaticMapUrl, longitude, latitude);
        Logger.d(TAG, url);
        RequestQueue queue = Volley.newRequestQueue(getContext());
        //TODO 未使用缓存，缓存可使用ImageLoader+LruCache实现
        ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                mapView.setImageBitmap(response);

            }
        }, 0, 0, ImageView.ScaleType.CENTER_INSIDE, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }

    protected void queryAddress(final TextView addressView, double latitude, double longitude) {
        Logger.d(TAG, "查询地理位置");
        GeoSearchUtils.queryLocation(latitude,longitude,new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
                if (i == 1000) {
                    RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
                    //省略了省市的地理位置
                    String address = regeocodeAddress.getFormatAddress()
                            .replace(regeocodeAddress.getProvince(), "")
                            .replace(regeocodeAddress.getCity(), "");
                    Logger.d(TAG, "查询到位置 ：" + address);
                    addressView.setText(address);
                }
            }

            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
            }
        });

    }
}
